package uutalk.netcloud.talk.base.util.thirdpart;

import android.content.Context;
import android.os.Bundle;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;

/**
 * Created by Pmt on 2017/7/16.
 */

public class SpeechSynManager {

    //语音播放
    private SpeechSynthesizer mTts;


    public SpeechSynManager(){

    }

    private static class SingleTonBuilder {
        private static SpeechSynManager singleTon = new SpeechSynManager();
    }

    public static SpeechSynManager getInstance(){
        return SingleTonBuilder.singleTon;
    }

    public void initSpeech(Context context){
        //科大讯飞
        // ①语音配置对象初始化
        SpeechUtility.createUtility(context, SpeechConstant.APPID + "=58fc0209");//将这里的578f1af7替换成自己申请得到的8位appid
        //initTTS(context);

    }

    public void initTTS(Context context){
        //1. 创建 SpeechSynthesizer 对象 , 第二个参数： 本地合成时传 InitListener
        mTts= SpeechSynthesizer.createSynthesizer(context, null);
        //2.合成参数设置，详见《 MSC Reference Manual》 SpeechSynthesizer 类
        //设置发音人（更多在线发音人，用户可参见 附录 13.2
        //设置引擎类型为本地
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
        //设置本地发音人
        mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
        mTts.setParameter(SpeechConstant.SPEED, "50");// 设置语速
        mTts.setParameter(SpeechConstant.VOLUME, "80");// 设置音量，范围 0~100
        mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端


    }

    public void simpleTextPlay(String msg){
        mTts.startSpeaking(msg, new SynthesizerListener() {
            @Override
            public void onSpeakBegin() {

            }

            @Override
            public void onBufferProgress(int i, int i1, int i2, String s) {

            }

            @Override
            public void onSpeakPaused() {

            }

            @Override
            public void onSpeakResumed() {

            }

            @Override
            public void onSpeakProgress(int i, int i1, int i2) {

            }

            @Override
            public void onCompleted(SpeechError speechError) {

            }

            @Override
            public void onEvent(int i, int i1, int i2, Bundle bundle) {

            }
        });

    }

    public void normalTextPlay(String msg, SynthesizerListener synthesizerListener){
        mTts.startSpeaking(msg, synthesizerListener);
    }

    public void releaseTTS()
    {
        if(mTts != null)
        {
            mTts.destroy();
        }
    }

    public void release()
    {

    }

}
